package com.yamaha.bikedesalon;

import java.util.LinkedHashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yamaha.bikedesalon.util.SCStaticFunc;

public class FindPartFragment extends AMyActivity {

	private final String TAG = this.getClass().getSimpleName();

	public FindPartFragment() {
		super(MenuType.FIND_PART);
	}

	@Override
	protected List<EditText> getEditTexts() {
		return null;
	}

	@Override
	public void onMyClick(View v) {
		switch (v.getId()) {
			case R.id.find_part_model_bg : {
				doShowModel();
			}
				break;
			case R.id.find_part_category_bg : {
				doShowCategory();
			}
				break;
			case R.id.find_part_dealer_bg : {
				doShowDealer();
			}
				break;
			case R.id.find_part_province_bg : {
				doShowProvince();
			}
				break;
			case R.id.general_filter_button : {
				doFilter();
			}
				break;
			case R.id.friend_filter_button : {
				doFriendFilter();
			}
				break;
			case R.id.like_filter_button : {
				doLikeFilter();
			}
				break;
			case R.id.dealer_filter_button : {
				doDealerOnlyFilter();
			}
				break;
			case R.id.user_filter_button : {
				doUserOnlyFilter();
			}
				break;
		}
	}

	private void doShowModel() {
		try {
			final JSONArray list = (JSONArray) SCStaticFunc.getData(this, SCStaticFunc.MODEL);

			final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
			for (int i = 0; i < list.length(); i++) {
				final JSONObject obj = list.getJSONObject(i);
				map.put(obj.getString("name"), obj.getString("model_id"));
			}

			final String[] items = map.keySet().toArray(new String[map.keySet().size()]);

			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Model");
			builder.setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

					final RelativeLayout modelButton = (RelativeLayout) findViewById(R.id.find_part_model_bg);
					final String modelId = map.get(items[which]);
					modelButton.setTag(modelId);

					final TextView modelTextView = (TextView) findViewById(R.id.model_spinner);
					modelTextView.setText(items[which]);

				}
			});
			builder.create().show();
		} catch (JSONException e) {
			LogUtil.e(this, TAG, e.getMessage(), e);
		}
	}

	private void doShowCategory() {
		try {
			final JSONArray list = (JSONArray) SCStaticFunc.getData(this, SCStaticFunc.CATEGORY);

			final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
			for (int i = 0; i < list.length(); i++) {
				final JSONObject obj = list.getJSONObject(i);
				map.put(obj.getString("name"), obj.getString("category_id"));
			}

			final String[] items = map.keySet().toArray(new String[map.keySet().size()]);

			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Category");
			builder.setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

					final RelativeLayout modelButton = (RelativeLayout) findViewById(R.id.find_part_category_bg);
					final String modelId = map.get(items[which]);
					modelButton.setTag(modelId);

					final TextView modelTextView = (TextView) findViewById(R.id.category_spinner);
					modelTextView.setText(items[which]);

				}
			});
			builder.create().show();
		} catch (JSONException e) {
			LogUtil.e(this, TAG, e.getMessage(), e);
		}
	}

	private void doShowDealer() {
		try {
			final JSONArray list = (JSONArray) SCStaticFunc.getData(this, SCStaticFunc.DEALER);

			final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
			for (int i = 0; i < list.length(); i++) {
				final JSONObject obj = list.getJSONObject(i);
				final String displayName;
				if (obj.getString("display_name").length() > 0) {
					displayName = obj.getString("display_name");
				} else {
					displayName = obj.getString("firstname");
				}
				map.put(displayName, obj.getString("user_id"));
			}

			final String[] items = map.keySet().toArray(new String[map.keySet().size()]);

			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Dealer");
			builder.setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

					final RelativeLayout button = (RelativeLayout) findViewById(R.id.find_part_dealer_bg);
					final String modelId = map.get(items[which]);
					button.setTag(modelId);

					final TextView modelTextView = (TextView) findViewById(R.id.dealer_spinner);
					modelTextView.setText(items[which]);

				}
			});
			builder.create().show();
		} catch (JSONException e) {
			LogUtil.e(this, TAG, e.getMessage(), e);
		}
	}

	private void doShowProvince() {
		try {
			final JSONArray list = (JSONArray) SCStaticFunc.getData(this, SCStaticFunc.PROVINCE);

			final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
			for (int i = 0; i < list.length(); i++) {
				final JSONObject obj = list.getJSONObject(i);
				map.put(obj.getString("name"), obj.getString("id"));
			}

			final String[] items = map.keySet().toArray(new String[map.keySet().size()]);

			final AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Dealer");
			builder.setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

					final RelativeLayout button = (RelativeLayout) findViewById(R.id.find_part_province_bg);
					final String modelId = map.get(items[which]);
					button.setTag(modelId);

					final TextView modelTextView = (TextView) findViewById(R.id.province_spinner);
					modelTextView.setText(items[which]);

				}
			});
			builder.create().show();
		} catch (JSONException e) {
			LogUtil.e(this, TAG, e.getMessage(), e);
		}
	}

	private void doFilter() {

		final RelativeLayout modelButton = (RelativeLayout) findViewById(R.id.find_part_model_bg);
		final RelativeLayout categoryButton = (RelativeLayout) findViewById(R.id.find_part_category_bg);
		final RelativeLayout dealerButton = (RelativeLayout) findViewById(R.id.find_part_dealer_bg);
		final RelativeLayout provinceButton = (RelativeLayout) findViewById(R.id.find_part_province_bg);

		final Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("model", (null != modelButton.getTag())
				? modelButton.getTag().toString()
				: "");
		intent.putExtra("category", (null != categoryButton.getTag())
				? categoryButton.getTag().toString()
				: "");
		intent.putExtra("dealer", (null != dealerButton.getTag())
				? dealerButton.getTag().toString()
				: "");
		intent.putExtra("province", (null != provinceButton.getTag())
				? provinceButton.getTag().toString()
				: "");
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(intent);
	}

	private void doLikeFilter() {
		final Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("extra", "LIKE");
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(intent);
	}

	private void doFriendFilter() {
		final Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("extra", "FRIEND");
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(intent);
	}

	private void doDealerOnlyFilter() {
		final Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("extra", "DEALER");
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(intent);
	}

	private void doUserOnlyFilter() {
		final Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("extra", "USER");
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		startActivity(intent);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.find_part_page;
	}

	@Override
	protected void onMyCreated(Bundle savedInstanceState) {
		final RelativeLayout modelButton = (RelativeLayout) findViewById(R.id.find_part_model_bg);
		final RelativeLayout categoryButton = (RelativeLayout) findViewById(R.id.find_part_category_bg);
		final RelativeLayout dealerButton = (RelativeLayout) findViewById(R.id.find_part_dealer_bg);
		final RelativeLayout provinceButton = (RelativeLayout) findViewById(R.id.find_part_province_bg);
		final ImageButton generalButton = (ImageButton) findViewById(R.id.general_filter_button);
		final ImageButton friendButton = (ImageButton) findViewById(R.id.friend_filter_button);
		final ImageButton likeButton = (ImageButton) findViewById(R.id.like_filter_button);
		final ImageButton dealerFilterButton = (ImageButton) findViewById(R.id.dealer_filter_button);
		final ImageButton userButton = (ImageButton) findViewById(R.id.user_filter_button);

		modelButton.setOnClickListener(this);
		categoryButton.setOnClickListener(this);
		dealerButton.setOnClickListener(this);
		provinceButton.setOnClickListener(this);
		generalButton.setOnClickListener(this);
		friendButton.setOnClickListener(this);
		likeButton.setOnClickListener(this);
		dealerFilterButton.setOnClickListener(this);
		userButton.setOnClickListener(this);
	}
}
